package de.upb.aquarium.api.objects;

import java.io.File;
import java.util.ArrayList;

import javax.media.opengl.GL;

import objimp.ObjImpScene;

/**
 * Animates a General3DObject.
 * Consists of several frames that can be switched by nextFrame().
 * @author Alexander
 */
public class AnimatedObject extends General3DObject {

	int currentFrame = 0;
	ArrayList<ObjImpScene> frameList = new ArrayList<ObjImpScene>();

	public AnimatedObject(GL gl, String filename, float scale) {
		String[] ends = filename.split("%X");
		int nr = 1;
		while (true) {
			String file = ends[0] + String.format("%06d", nr++) + ends[1];
			if (!new File(file).exists())
				break;
			System.out.println("Loading file: "+file);
			ObjImpScene obj = new ObjImpScene(gl);
			obj.load(file, scale);
			frameList.add(obj);
		}
	}

	@Override
	public void paint(GL gl) {
		beforeRendering(gl);
		frameList.get(currentFrame).draw(gl);
		afterRendering(gl);
		nextFrame();
	}

	public void nextFrame() {
		if (++currentFrame > frameList.size() - 1)
			currentFrame = 0;
	}

}
